package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.szewec.ps.R;
import com.szewec.ps.base.BaseApplication;
import com.szewec.ps.base.Constants;
import com.szewec.ps.util.StringUtils;

import java.math.BigDecimal;

public class InputItem extends ConstraintLayout implements BaseInput {
    protected TextView itemName;
    protected EditText inputType;
    protected LinearLayout selectType;
    protected TextView selectValue;

    /**
     * 私有属性,临时变量
     */
    private ImageView ivRight;
    private CharSequence hint;
    private OnClickListener listener;

    public InputItem(Context context) {
        super(context);
        init(context, null);
    }

    public InputItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.input_item_view, this);
        itemName = findViewById(R.id.tv_item_name);
        inputType = findViewById(R.id.type_input);
        selectType = findViewById(R.id.type_select);
        selectValue = findViewById(R.id.select_value);
        ivRight = findViewById(R.id.iv_right);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputItem);
            String name = ta.getString(R.styleable.InputItem_itemName);
            itemName.setText(name);
            String h = ta.getString(R.styleable.InputItem_itemHint);
            if (StringUtils.isNotEmpty(h)) {
                inputType.setHint(h);
            }

            boolean required = ta.getBoolean(R.styleable.InputItem_required, false);

            if (!required) {
                itemName.setCompoundDrawables(null, null, null, null);
            }


            String type = ta.getString(R.styleable.InputItem_type);

            int customType = ta.getInt(R.styleable.InputItem_inputType, -1);
            if (customType != -1) {
                inputType.setInputType(EditorInfo.TYPE_CLASS_NUMBER | customType);
                //如果hint为空,设置为数字类型的hint
                if (StringUtils.isEmpty(h)) {
                    inputType.setHint(getResources().getString(R.string.input_type_number));
                }
            }
            //记录原始hint
            hint = inputType.getHint();
            if (Constants.InputType.SELECT.equals(type)) {
                inputType.setVisibility(GONE);
                selectType.setVisibility(VISIBLE);
                //如果是选择类型,设置为选择类型的hint
                hint = selectValue.getHint();
            }
            String value = ta.getString(R.styleable.InputItem_defaultValue);
            setValue(value);
            ta.recycle();
        }
    }

    public String getStringValue() {
        String value = null;
        if (inputType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(inputType.getText().toString())) {
                value = inputType.getText().toString();
            }
        } else if (selectType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(selectValue.getText().toString())) {
                value = selectValue.getText().toString();
            }
        }
        return value;
    }

    public Double getDoubleValue() {
        Double value = null;
        if (inputType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(inputType.getText().toString())) {
                value = Double.valueOf(inputType.getText().toString());
            }
        } else if (selectType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(selectValue.getText().toString())) {
                value = Double.valueOf(selectValue.getText().toString());
            }
        }
        return value;
    }

    public BigDecimal getBigDecimalValue() {
        BigDecimal value = null;
        if (inputType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(inputType.getText().toString())) {
                value = new BigDecimal(inputType.getText().toString());
            }
        } else if (selectType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(selectValue.getText().toString())) {
                value = new BigDecimal(selectValue.getText().toString());
            }
        }
        return value;
    }

    public Integer getIntValue() {
        Integer value = null;
        if (inputType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(inputType.getText().toString())) {
                value = Integer.valueOf(inputType.getText().toString());
            }
        } else if (selectType.getVisibility() == VISIBLE) {
            if (StringUtils.isNotEmpty(selectValue.getText().toString())) {
                value = Integer.valueOf(selectValue.getText().toString());
            }
        }
        return value;
    }

    public void setValue(Object value) {
        if (value == null) {
            return;
        }
        if (inputType.getVisibility() == VISIBLE) {
            inputType.setText(value.toString());
        } else if (selectType.getVisibility() == VISIBLE) {
            selectValue.setText(value.toString());
        }
    }

    /**
     * 设置是否必填
     *
     * @param required
     */
    public void setRequired(boolean required) {
        if (required) {
            Drawable drawable = ContextCompat.getDrawable(BaseApplication.getContext(), R.drawable.red_star);
            itemName.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        } else {
            itemName.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
    }

    /**
     * 设置enable
     *
     * @param enabled
     */
    public void setEnabled(boolean enabled) {
        selectType.setEnabled(enabled);
    }

    /**
     * 设置选择类型点击事件
     *
     * @param listener
     */
    public void setSelectTypeClickListener(OnClickListener listener) {
        this.listener = listener;
        selectType.setOnClickListener(listener);
    }

    /**
     * textChange事件
     *
     * @param watcher
     */
    public void setTextChangeListener(TextWatcher watcher) {
        if (watcher == null) {
            return;
        }
        if (inputType.getVisibility() == VISIBLE) {
            inputType.addTextChangedListener(watcher);
        } else if (selectType.getVisibility() == VISIBLE) {
            selectValue.addTextChangedListener(watcher);
        }
    }

    /**
     * 获取选择类型input的TextView
     *
     * @return
     */
    public TextView getSelectTextView() {
        return selectValue;
    }

    /**
     * 切换textview
     */
    @Override
    public void switchoverMode(boolean isEnabled) {
        if (isEnabled) {
            inputType.setEnabled(true);
            selectValue.setEnabled(true);
            selectValue.setOnClickListener(listener);
            inputType.setHint(hint);
            selectValue.setHint(hint);
            ivRight.setVisibility(VISIBLE);
            if (itemName.getText().equals("录入时间") || itemName.getText().equals("录入人员")) {
                this.setVisibility(GONE);
            }
        } else {
            inputType.setEnabled(false);
            selectValue.setEnabled(false);
            selectValue.setOnClickListener(null);
//            inputType.setHint("");
            selectValue.setHint("");
            ivRight.setVisibility(GONE);
            if (itemName.getText().equals("录入时间") || itemName.getText().equals("录入人员")) {
                this.setVisibility(VISIBLE);
            }
        }
    }

    public void setItemName(String itemName) {
        this.itemName.setText(itemName);
    }
}
